package com.kou.controller;/**
 * @author SteinY
 * @create 2022-03-24 21:32
 * @description:
 */

import com.kou.pojo.User;
import com.kou.service.impl.AdminServiceImple;
import com.kou.utils.Constants;
import com.kou.utils.StrUtils;
import com.qf.service.AdminService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * projectName:myshop2
 * author:SteinY
 * time:2022/3/24 21:32
 * description:
 */
@WebServlet("/admin")
public class AdminServlet extends BaseServlet {
    AdminService adminService = new AdminServiceImple();
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if(!StrUtils.empty(username)){
            request.setAttribute("msg","用户名不能为空！");
            return Constants.FORWARD + "message.jsp";
        }
        if(!StrUtils.empty(password)){
            request.setAttribute("msg","密码不能为空！");
            return Constants.FORWARD + "message.jsp";
        }
        User user = adminService.login(username,password);
        if (user==null){
            request.setAttribute("msg","账号或者密码错误！");
            return Constants.FORWARD + "message.jsp";
        }
        request.getSession().setAttribute("admin",user);
        return Constants.FORWARD + "admin/admin.jsp";
    }

    public String logOut(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("admin");


        return Constants.REDIRECT + "admin/admin.jsp";
    }
}